﻿using System;

namespace wojilu.Data
{
    /// <summary>
    /// sqlite 特殊语法处理器
    /// </summary>
    public class SqliteDialect : IDatabaseDialect
    {
        public String GetConnectionItem(String connectionString, ConnectionItemType connectionItem)
        {
            return getConnectionItem(connectionString, connectionItem);
        }


        public static String getConnectionItem(String connStr, ConnectionItemType connectionItemType)
        {

            if (strUtil.IsNullOrEmpty(connStr)) throw new Exception("sqlite connection string is empty");

            String[] arrItems = connStr.ToLower().Split(';');

            foreach (String item in arrItems)
            {

                if (strUtil.IsNullOrEmpty(item)) continue;
                String[] arrPair = item.Split('=');
                if (arrPair.Length != 2) continue;

                String key = arrPair[0].Trim();
                String val = arrPair[1].Trim();

                if (keyEqual(key, connectionItemType)) return val;

            }

            return "";
        }

        private static Boolean keyEqual(String key, ConnectionItemType connectionItemType)
        {

            if (connectionItemType == ConnectionItemType.Database) return key == "data source";
            return false;
        }

        public String GetLimit(String sql, int limit)
        {
            return (sql + " limit " + limit);
        }

        public String GetLimit(String rSql)
        {
            if (rSql == null) return null;
            String sql = rSql.ToLower();
            if (sql.ToLower().IndexOf(" top ") > 0)
            {

                // selec top 10 * from
                // 10 * from
                String rMainSql = sql.Split(new String[] { " top " }, StringSplitOptions.None)[1].Trim();

                String[] arrItem = rMainSql.Split(' ');
                int limit = cvt.ToInt(arrItem[0]);

                String mainSql = strUtil.TrimStart(rMainSql, arrItem[0]);


                return this.GetLimit("select " + mainSql, limit);
            }
            throw new SystemException(sql);
            return sql;
        }

        public String GetTimeQuote()
        {
            return "'";
        }

        public String GetParameter(String parameterName)
        {
            return ("@" + parameterName);
        }

        public String GetParameterAdder(String parameterName)
        {
            return ("@" + parameterName);
        }

        public String Top
        {
            get { return "limit"; }
        }

        public string GetLeftQuote()
        {
            return "`";
        }

        public string GetRightQuote()
        {
            return "`";
        }

    }
}
